// pages/xiugai/xiugai.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['--请选择--', ],
    index: 0,
    classify:'',
    popup:true,
    array2: ['--请选择--', ],
    index2: 0,
    content: '',
    
    hiddenmodalput: true,
    nub: 0,
    whid: 0,
  },
  bindPickerChange: function (e) {

    this.setData({
      index: e.detail.value,
      index2:0,
    })
    let typeid= this.data.classify_id[this.data.index]
    if (typeid != 0) {
      this.goods(typeid)
      
    } else {
      this.setData({
        array2: ['--请选择--', ],
      })
    }


  },
  goods(e) {
    console.log(e)
    let typeid = e;
    wx.request({
      url: 'http://39.106.9.207:8080/warehouse/GetGoods',
      data: {
        whid: this.data.whid,
        typeid:typeid,
      },
      header: {
        'content-type': 'multipart/form-data' // 默认值
      },
      success: (res) => {
        console.log(res.data);
        let array2 = ['--请选择--', ];
        var i = 0;
        while (i < res.data.length) {
          // console.log(res.data[i].name)
          array2.push(res.data[i].name)
          i++;
        }
        this.setData({
          array2: [],
          array2: array2,
        })
      }
    })
  },
  bindPicker: function (e) {
    this.setData({
      index2: e.detail.value
    })
  },
  fanhui: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  modalinput: function () {
    if (this.data.classify_id[this.data.index] != 0) {
      this.setData({
        hiddenmodalput: !this.data.hiddenmodalput
      })
    } else {
      wx.showToast({
        title: '请选择分类',
        icon: 'error',
        duration: 2000
      })
    }

  },
  // 添加成功弹窗
  addition() {
    wx.showToast({
      title: '添加成功',
      icon: 'success',
      duration: 1500
    })
  },
  //删除成功弹窗
  delete() {
    wx.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 1500
    })
  },
  // 删除物品
  remove: function (e) {
    let array2 = this.data.array2;
    let Index = e.target.dataset.index;
    let goodsname = array2[Index];
    let typename = this.data.array[this.data.index];
    // console.log(e.target.dataset.index);
    console.log(array2[Index]);
    if (Index != 0) {
      wx.showModal({
        title: '提示',
        content: '你是否要删除该物品',
        success: (res) => {
          if (res.confirm) {
            wx.request({
              url: 'http://39.106.9.207:8080/warehouse/GoodsDel',
              data: {
                typename: typename,
                goodsname: goodsname,
                whid: this.data.whid,
              },
              method: 'POST',
              header: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
              },
              success: (res) => {
                console.log(res.data)
                this.delete();
              }
            })
            array2.splice(Index, 1)
            // console.log(array2)
            this.setData({
              array2,
              index2: 0,
            });
          } else if (res.cancel) {}
        }
      })
    } else {
      wx.showToast({
        title: '不可删除',
        icon: 'error',
        duration: 1000
      })
      // setTimeout(function () {
      //   wx.hideLoading()
      // }, 1000)
    }
  },
  //输入的添加分类名称
  classify:function(e){
    this.setData({
      classify: e.detail.value
    })
  },
  //输入的添加物品名称
  content: function (e) {
    this.setData({
      content: e.detail.value
    })
    // console.log(this.data.content)
  },
  //取消按钮分类
  cancel_2: function () {
    this.setData({
      popup: true
    });
  },
  //确认添加分类
  confirm_2: function () {
    let classify = this.data.classify.trim();
    let reg = /\S/;
    let array = this.data.array;
    console.log(reg.test(classify));
    if (reg.test(classify) == true) {
      let whid = this.data.whid;
      wx.request({
        url: 'http://39.106.9.207:8080/warehouse/TypeAdd',
        data: {
          name: classify,
          whid: whid,
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
        },
        success: (res) => {
          // console.log(res.data)
          //成功弹窗提醒
          if (res.data == '添加成功') {
            this.addition();
            array.push(classify);
            this.setData({
              array,
              popup: true,
              classify: '',
            })
          }else{
            wx.showToast({
              title: '分类已存在',
              icon: 'error',
              duration: 1500
            })
          }

        }
      })

      wx.request({
        url: 'http://39.106.9.207:8080/warehouse/GetType',
        data: {
          whid: this.data.whid,
        },
        header: {
          'content-type': 'multipart/form-data' // 默认值
        },
        success: (res) => {
          console.log(res.data);
          let array = ['--请选择--', ];
          let classify_id = [0];
          var i = 0;
          while (i < res.data.length) {
            // console.log(res.data[i].name)
            array.push(res.data[i].name)
            classify_id.push(res.data[i].id)
            i++;
          }
          this.setData({
            array: array,
            classify_id
          })
          // console.log(this.data.classify_id)
        }
      })
    } else {
      wx.showToast({
        title: '内容不能为空',
        icon: 'error',
        duration: 1500
      })
    }
  },
  //取消按钮
  cancel: function () {
    this.setData({
      hiddenmodalput: true
    });
  },
  //确认添加物品
  confirm: function () {
    let content = this.data.content.trim();
    let array2 = this.data.array2;
    let reg = /\S/;
    // console.log(reg.test(content));
    if (reg.test(content) == true) {
      //   
      let typename = this.data.array[this.data.index];
      let content = this.data.content;
      let whid = this.data.whid;
      // console.log(content);
      // console.log(whid);
      wx.request({
        url: 'http://39.106.9.207:8080/warehouse/GoodsAdd',
        data: {
          typename: typename,
          goodsname: content,
          whid: whid,
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
        },
        success: (res) => {
          // console.log(res.data)
          //成功弹窗提醒
          if (res.data != '物品已存在') {
            this.addition();
            array2.push(content);
            this.setData({
              array2,
              hiddenmodalput: true,
              content: '',
            })
          }else{
            wx.showToast({
              title: '物品已存在',
              icon: 'error',
              duration: 1500
            })
          }

        }
      })

    } else {
      wx.showToast({
        title: '内容不能为空',
        icon: 'error',
        duration: 1500
      })
    }
  },
  //添加分类
  new_classify:function(){
    this.setData({
      popup: !this.data.popup
    })
  },
  //删除分类
  del_classify(e){
    let array = this.data.array;
    let index = e.target.dataset.index;
    let goodsname = array[index];
    if (index != 0) {
      wx.showModal({
        title: '提示',
        content: '你是否要删除该分类',
        success: (res) => {
          if (res.confirm) {
            wx.request({
              url: 'http://39.106.9.207:8080/warehouse/TypeDel',
              data: {
                name: goodsname,
                whid: this.data.whid,
              },
              method: 'POST',
              header: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
              },
              success: (res) => {
                // console.log(res.data)
                if (res.data == '删除成功') {
                  this.delete();
                  array.splice(index, 1)
                  this.setData({
                    array,
                    index:0,
                  });
                }else{
                  wx.showToast({
                    title: '该分类下有物品',
                    icon: 'error',
                    duration: 1000
                  })
                }
                
              }
            })
            
          } else if (res.cancel) {}
        }
      })
    } else {
      wx.showToast({
        title: '不可删除',
        icon: 'error',
        duration: 1000
      })
      // setTimeout(function () {
      //   wx.hideLoading()
      // }, 1000)
    }
  },
  onLoad: function (options) {
    if (wx.getStorageSync('warehouseId') != 0) {
      let whid = wx.getStorageSync('warehouseId')
      this.setData({
        whid: whid
      })
      console.log(this.data.whid)
    };
    wx.request({
      url: 'http://39.106.9.207:8080/warehouse/GetType',
      data: {
        whid: this.data.whid,
      },
      header: {
        'content-type': 'multipart/form-data' // 默认值
      },
      success: (res) => {
        console.log(res.data);
        let array = ['--请选择--', ];
        let classify_id = [0];
        var i = 0;
        while (i < res.data.length) {
          // console.log(res.data[i].name)
          array.push(res.data[i].name)
          classify_id.push(res.data[i].id)
          i++;
        }
        this.setData({
          array: array,
          classify_id
        })
        // console.log(this.data.classify_id)
      }
    })
  },
})